package com.jic.market.impl.skill;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.entity.skill.ActivityStoreConfig;
import com.jic.market.mapper.skill.ActivityStoreConfigMapper;
import com.jic.market.request.CouponDetailRequest;
import com.jic.market.request.manager.base.PromotionIdRequest;
import com.jic.market.service.skill.ActivityStoreConfigService;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 活动店铺基础配置表
 *
 * @author : seven
 * @version : v1.0.0
 * @email : 774329481@qq.com
 * @since : 2020-03-12 05:30:04
 */
@Service
public class ActivityStoreConfigServiceImpl implements ActivityStoreConfigService {

    @Autowired
    private ActivityStoreConfigMapper activityStoreConfigMapper;

    @Override
    public long insert(ActivityStoreConfig activityStoreConfig) {
        activityStoreConfig.setDeleteFlag(false);
        long flag = activityStoreConfigMapper.insert(activityStoreConfig);
        return flag;
    }

    @Override
    public long delete(String promotionId) {
        return activityStoreConfigMapper.delete(promotionId);
    }

    @Override
    public long update(ActivityStoreConfig activityStoreConfig) {
        return activityStoreConfigMapper.update(activityStoreConfig);
    }

    @Override
    public ActivityStoreConfig queryByPrimaryKey(Long id) {
        return activityStoreConfigMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<ActivityStoreConfig> query(ActivityStoreConfig activityStoreConfig) {
        return activityStoreConfigMapper.query(activityStoreConfig);
    }

    @Override
    public PageResult<ActivityStoreConfig> queryPage(ActivityStoreConfig activityStoreConfig, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ActivityStoreConfig> list = activityStoreConfigMapper.queryPage(startIndex, pageQuery.getLimit(), activityStoreConfig);
        long count = activityStoreConfigMapper.queryPageCount(activityStoreConfig);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public List<ActivityStoreConfig> queryStoreByPromotionId(String promotionId, Integer start , Integer limit,List<Long> authStoreIds, CouponDetailRequest promotionIdRequest) {
        if(start != null && limit != null){
            start = (start - 1) * limit;
        }
        return activityStoreConfigMapper.queryStoreByPromotionId(promotionId,start,limit,authStoreIds, promotionIdRequest);
    }

    @Override
    public List<String> queryStoreIdByPromotionId(String promotionId) {
        return activityStoreConfigMapper.queryStoreIdByPromotionId(promotionId);
    }

    @Override
    public long deleteStoreByPromotionIdAndMerchantId(String promotionId, String merchantId) {
        return activityStoreConfigMapper.deleteStoreByPromotionIdAndMerchantId(promotionId,merchantId);
    }

    @Override
    public long deleteStoreByPromotionIdAndStoreId(String promotionId, String storeId) {
        return activityStoreConfigMapper.deleteStoreByPromotionIdAndStoreId(promotionId,storeId);
    }

    @Override
    public boolean copyStore(PromotionIdRequest promotionIdRequest, String newPromotionId) {
        List<ActivityStoreConfig> storeConfigList = queryStoreByPromotionId(promotionIdRequest.getPromotionId(),null,null, Collections.emptyList(),null);
        if (CollectionUtils.isEmpty(storeConfigList)) {
            return true;
        }
        storeConfigList.stream().forEach(temp -> {
            temp.setPromotionId(newPromotionId);
            temp.setDeleteFlag(false);
            //insert(temp);
        });
        activityStoreConfigMapper.insertBatch(storeConfigList);
        return true;
    }

    @Override
    public Long queryByPromotionIdTotal(String promotionId,List<Long> authStoreIds, CouponDetailRequest promotionIdRequest) {
        return activityStoreConfigMapper.queryByPromotionIdTotal(promotionId,authStoreIds, promotionIdRequest);
    }

    @Override
    public List<Long> getStoreIdsByPromotionId(String promotionId) {
        return activityStoreConfigMapper.getStoreIdsByPromotionId(promotionId);
    }

    @Override
    public RestResult<List<Long>> getCouponStoreIdsByPromotionId(PromotionIdRequest promotionIdRequest) {
        return RestResult.success(activityStoreConfigMapper.getCouponStoreIdsByPromotionId(promotionIdRequest.getPromotionId()));
    }
}